package com.multicampus.minishop.view.product;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.disk.DiskFileItem;

import com.multicampus.minishop.framework.Action;
import com.multicampus.minishop.product.vo.ProductVO;
import com.multicampus.minishop.services.product.ProductService;
import com.multicampus.minishop.services.product.impl.ProductServiceImpl;
import com.multicampus.minishop.util.GuysUtil;

public class UpdateProductAction extends Action {

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String name = null;
		String detail = null;
		String date = null;
		int price = 0;
		int prodNo = 0;
		String fileName = null;
		ProductVO vo = new ProductVO();

		if (FileUpload.isMultipartContent(request)) {
			DiskFileUpload fileUpload = new DiskFileUpload();

			List fileItemList = fileUpload.parseRequest(request);
			for (int i = 0; i < fileItemList.size(); i++) {

				DiskFileItem fileItem = (DiskFileItem) fileItemList.get(i);

				if (fileItem.isFormField()) {
					if (fileItem.getFieldName().equals("prodName")) {
						name = fileItem.getString("euc-kr");

					} else if (fileItem.getFieldName().equals("prodDetail")) {
						detail = fileItem.getString("euc-kr");
					} else if (fileItem.getFieldName().equals("manuDate")) {
						date = fileItem.getString("euc-kr");
					} else if (fileItem.getFieldName().equals("price")) {
						price = Integer.parseInt(fileItem.getString());
					} else if (fileItem.getFieldName().equals("prodNo")) {
						prodNo = Integer.parseInt(fileItem.getString());
					}
					
				} else {
					if (fileItem.getSize() > 0) {
						int idx = fileItem.getName().lastIndexOf("\\");
						if (idx == -1) {
							idx = fileItem.getName().lastIndexOf("/");
						}

						fileName = fileItem.getName().substring(idx + 1);
						try {
							String uploadAbsPath = "C:/Program Files/uploadFile";
							File uploadedFile = new File(uploadAbsPath,
									fileName);
							File tempFile = new File(uploadAbsPath);
							System.out.println(tempFile.getAbsolutePath());
							if (!tempFile.exists()) {
								tempFile.mkdirs();
							}
							fileItem.write(uploadedFile);
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
			vo.setProdNo(prodNo);
			vo.setProdName(name);
			vo.setProdDetail(detail);
			vo.setManuDate(date);
			vo.setPrice(price);
			vo.setFileName(fileName);
		}
		System.out.println("prodNo : " + vo.getProdNo());

		ProductService service = new ProductServiceImpl();
		ProductVO pvo = service.updateProduct(vo);
		request.setAttribute("vo", pvo);

		return "redirect:/showDetailProduct.chic?prodNo=" + prodNo;

	}

}
